package com.whmnx.chitchat.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.whmnx.chitchat.R;
import com.whmnx.chitchat.bean.DynaminBean;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.main.MainActivity;
import com.whmnx.chitchat.utils.SPUtil;
import com.whmnx.chitchat.utils.UItils;

import java.util.ArrayList;
import java.util.List;

/**
 * author : huang
 * date   : 2020/7/27 0027 17:31
 * version: 1.0
 * @author Administrator
 */
public class CommentAdapter extends CommonRecyclerAdapter<DynaminBean.ForumListBean.CommentBean> {
    private Context context;
    private List<Boolean> alreadyLikeList = new ArrayList<>();
    private ImageView view;

    private DynamicAdapter.ReplyListener mReplyListener;

    public void setReplyListener(DynamicAdapter.ReplyListener replyListener) {
        mReplyListener = replyListener;
    }




    public interface ReplyListener {
        //  void commentReplyTopUser(DynamicCommentBean item, int position);

        void commentReplyUser(DynaminBean.ForumListBean item, int position);

        //   void dianz(DynamicCommentBean item, int position);
    }


    public CommentAdapter(Context context) {
        super(context, R.layout.item_list_like);
        this.context = context;
    }


    @Override
    public void onUpdate(BaseAdapterHelper helper, DynaminBean.ForumListBean.CommentBean item, int position) {
        TextView tv = helper.getView(R.id.tv_name);
        String userId = SPUtil.getString(Constants.USERID);
        if (TextUtils.isEmpty(item.ForumComment_NikeName)){
          //  helper.setText(R.id.tv_name, item.UserInfo_NickName + "：" + item.ForumComment_Content);
            SpannableString ss = new SpannableString(item.UserInfo_NickName + "："+ item.ForumComment_Content);
            //点击事件
            ClickableSpan clickableSpan = new ClickableSpan() {
                @Override
                public void onClick(View view) {
                    if (item.UserInfo_ID.equals(userId)){
                        UItils.showToastSafe("自己不能评论自己");
                        return;
                    }
                    if (mReplyListener != null) {
                        mReplyListener.commentReplyUser(item.Forum_ID,item.ForumComment_ID,item,position);
                    }
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    ds.setUnderlineText(false);
                }
            };
            //粗体
                StyleSpan styleSpan = new StyleSpan(Typeface.BOLD);
                if (!TextUtils.isEmpty(item.UserInfo_NickName)) {
                    ss.setSpan(clickableSpan, 0, item.UserInfo_NickName.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                    ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#000000"));
                    ss.setSpan(foregroundColorSpan, 0, item.UserInfo_NickName.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                    ss.setSpan(styleSpan, 0, item.UserInfo_NickName.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                    tv.setText(ss);
                    tv.setMovementMethod(LinkMovementMethod.getInstance());
                }

        }else {
          //  helper.setText(R.id.tv_name, item.ForumComment_NikeName + "回复" + item.UserInfo_NickName + "：" + item.ForumComment_Content);
          //  helper.setText(R.id.tv_name, item.ForumComment_NikeName + "回复" +item.UserInfo_NickName + "：" + item.ForumComment_Content);

            SpannableStringBuilder ss = new SpannableStringBuilder();
            if (!TextUtils.isEmpty(item.UserInfo_NickName))
                ss.append(item.UserInfo_NickName + "回复" + item.ForumComment_NikeName + "：" + item.ForumComment_Content);
            ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#000000"));

            //点击事件
            ClickableSpan clickableSpan = new ClickableSpan() {
                @Override
                public void onClick(View view) {
                    if (TextUtils.isEmpty(item.UserInfo_ID)){
                        return;
                    }
                      if (item.UserInfo_ID.equals(userId)){
                          UItils.showToastSafe("自己不能评论自己");
                          return;
                      }
                    if (mReplyListener != null) {
                        mReplyListener.commentReplyUser(item.Forum_ID,item.ForumComment_ID,item,position);
                    }
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    ds.setUnderlineText(false);
                }
            };
            //点击事件
            ClickableSpan clickableSpan2 = new ClickableSpan() {
                @Override
                public void onClick(View view) {
                   /* if (TextUtils.isEmpty(item.ForumComment_UserID)){
                        return;
                    }
                    if (item.ForumComment_UserID.equals(userId)){
                        UItils.showToastSafe("自己不能评论自己");
                        return;
                    }*/
                   /* if (mReplyListener != null) {
                            mReplyListener.commentReplyUser(item.Forum_ID, item.ForumComment_ID, item, position);
                    }*/
                }
                @Override
                public void updateDrawState(TextPaint ds) {
                    ds.setUnderlineText(false);
                }
            };
            //粗体
            if (!TextUtils.isEmpty(item.UserInfo_NickName) && !TextUtils.isEmpty(item.ForumComment_NikeName)) {
                StyleSpan styleSpan = new StyleSpan(Typeface.BOLD);
                ss.setSpan(styleSpan, 0, item.UserInfo_NickName.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                ss.setSpan(styleSpan, item.UserInfo_NickName.length() + 2, item.ForumComment_NikeName.length() + 2 + item.UserInfo_NickName.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                ss.setSpan(clickableSpan, 0, item.UserInfo_NickName.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                ss.setSpan(clickableSpan2, item.UserInfo_NickName.length() + 2, item.ForumComment_NikeName.length() + 2 + item.UserInfo_NickName.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                ss.setSpan(foregroundColorSpan, 0, item.UserInfo_NickName.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                ForegroundColorSpan foregroundColorSpan2 = new ForegroundColorSpan(Color.parseColor("#000000"));
                ss.setSpan(foregroundColorSpan2, item.UserInfo_NickName.length() + 2, item.ForumComment_NikeName.length() + 2 + item.UserInfo_NickName.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                tv.setText(ss);
                tv.setMovementMethod(LinkMovementMethod.getInstance());

                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (TextUtils.isEmpty(item.UserInfo_ID)){
                            return;
                        }
                        if (item.UserInfo_ID.equals(userId)){
                            UItils.showToastSafe("自己不能评论自己");
                            return;
                        }
                        if (mReplyListener != null) {
                            mReplyListener.commentReplyUser(item.Forum_ID, item.ForumComment_ID, item, position);
                        }
                    }
                });
            }
        }


       /* helper.getView(R.id.tv_name).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mReplyListener != null) {
                  //  mReplyListener.commentReplyUser(item.Forum_ID,item.ForumComment_ID,position);
                }
            }
        });*/
    }
}
